package com.project.pojo;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

public class Main {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchFieldException {
        /**
         * 使用Class类获得反射的三种方式
         * 1.对象.class
         * 2.new 对象().getClass();
         * 3.Class.forName();
         */
        Class<User> clazz = User.class;
        System.out.println(clazz.getName());

        Class<User> clazz1 = (Class<User>) new User().getClass();
        System.out.println(clazz1.getName());

        Class clazz2 = Class.forName("com.project.pojo.User");
        System.out.println(clazz2.getName());
        User user = (User) clazz2.newInstance();
        user.setId(1);
        System.out.println(user.getId());


        /**
         * 获得对象的属性名,属性权限,属性类型
         */
        Field field = clazz.getDeclaredField("username");

        System.out.println(Modifier.toString(field.getModifiers())+" "+field.getType().getSimpleName()+" "+field.getName()+";");

        Field[] fields = clazz.getDeclaredFields();
        for (Field field1 : fields) {
            System.out.println(Modifier.toString(field1.getModifiers())+" "+field1.getType().getSimpleName()+" "+field1.getName()+";");
        }


    }
}
